<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">二维码生成</p>
    </div>
    <div class="view-container">
        <p>可下载数量：{{ data.downloadQuantity }}<span style="margin-left: 20px;">已下载数量：</span>{{ data.generatedQuantity }}</p>
        <!-- 母码生成 -->
        <h3 class="code-title">母码</h3>
        <div class="code-num">
          母码数量：<el-input v-model.number="inputFath" placeholder="请输入母码的数量" class="code-input"></el-input>
          <el-button type="primary" @click="getMotherCode()" class="btn-success"><img src="@/assets/img/Quickcode/quickcode.png" class="icon-download"/>生成母码</el-button>
        </div>
        <p class="code-result">生成结果：{{ parseTime(new Date()) }} 生成数量 {{ inputFath }}个</p>
        <h3 class="code-title mt50">子码</h3>
        <div class="code-num">
          子码数量：<el-input v-model.number="inputSon" placeholder="请输入子码的数量" class="code-input"></el-input>
          <el-button type="primary" @click="getSubCode()" class="btn-success" ><img src="@/assets/img/Quickcode/quickcode.png" class="icon-download"/>生成子码</el-button>
        </div>
        <p class="code-result">生成结果：{{ parseTime(new Date()) }} 生成数量 {{ inputSon }} 个</p>
        <h3 class="code-title mt50">托盘码</h3>
        <div class="code-num">
          托盘码数量：<el-input v-model.number="inputPallet" placeholder="请输入托盘码的数量" class="code-input"></el-input>
          <el-button type="primary" @click="getPalletCode()" class="btn-success" ><img src="@/assets/img/Quickcode/quickcode.png" class="icon-download"/>生成托盘码</el-button>
        </div>
        <p class="code-result">生成结果：{{ parseTime(new Date()) }} 生成数量 {{ inputSon }} 个</p>
      </div>
  </div>
</template>

<script>
import { motherCode, subCode, numberCode, codeLimit, palletCode } from  "@/api/system/quickcode/quickcode";
export default {
  name: "quickcode",
  data() {
    return {
      // 遮罩层
      loading: true,
      inputFath:'',
      inputSon:'',
      inputPallet: '',
      data: ''
    };
  },
  created() {
    this.getCodeLimit()
  },
  methods: {
    // 
    getCodeLimit(){
      codeLimit().then(res => {
        this.data = res.data
      })
    },
    // 下载托盘码
    getPalletCode(){
      if(this.inputPallet > 0){
        numberCode(this.inputPallet).then(res => {
          if(res.data == '0'){
            this.loading = true
            palletCode(this.inputPallet).then((response)=>{
              // this.motherCode = response.rows
              // this.total = response.total
              this.loading = false
              var data = response
              let str = new Blob([data], {type: 'text/plain;charset=utf-8'})
              saveAs(str, `托盘码.txt`)
              this.inputPallet = ''
            })
          }else{
            this.$modal.msgError('生成码数量超过限制，请联系管理员!')
          }
        })
        
      }else{
        this.$modal.msgError('数量必须大于0!')
        this.inputPallet = ''
      }
    },
    // 下载母码
    getMotherCode(){
      if(this.inputFath > 0){
        numberCode(this.inputFath).then(res => {
          if(res.data == '0'){
            this.loading = true
            motherCode(this.inputFath).then((response)=>{
              // this.motherCode = response.rows
              // this.total = response.total
              this.loading = false
                var data = response
              let str = new Blob([data], {type: 'text/plain;charset=utf-8'})
              saveAs(str, `母码.txt`)
              this.inputFath = ''
            })
          }else{
            this.$modal.msgError('生成码数量超过限制，请联系管理员!')
          }
        })
        
      }else{
        this.$modal.msgError('数量必须大于0!')
        this.inputFath = ''
      }
    },
    // 下载子码
    getSubCode(){
      if(this.inputSon > 0){
        numberCode(this.inputSon).then(res => {
          if(res.data == '0'){
            this.loading = true
            subCode(this.inputSon).then((response)=>{
              // this.sonCode = response.rows
              // this.total = response.total
              this.loading = false
              var data = response
              let son = new Blob([data], {type: 'text/plain;charset=utf-8'})
              saveAs(son, `子码.txt`)
              this.inputSon = ''
            })
          }else{
            this.$modal.msgError('生成码数量超过限制，请联系管理员!')
          }
        })
      }else{
        this.$modal.msgError('数量必须大于0!')
        this.inputSon = ''
      }
    }
  }
}
</script>

<style scoped>
.btn-success{
  position: relative;
  width: 138px;
  height: 36px;
  background: #28C6C6;
  opacity: 0.75;
  font-size: 18px;
  text-indent: 10px;
}
.code-title{
  line-height: 60px;
}
.code-num{
  display: flex;
  align-items: center;
}
.code-input{
  width: 570px;
  margin-right: 10px;
}
.code-result{
  margin-top: 20px;
}
.icon-download{
  position: absolute;
  top: 11px;
  left: 15px;
  width: 16px;
  height: 16px;
}
</style>
